<?
header('Cache-control: private, must-revalidate');
header('Content-type: text/html; charset=ISO-8859-1');
$euro = " ".utf8_encode(chr(128));
// $euro = "<img src='/images/euro.gif'/>";

foreach($factures as $n=>$facture){
	$typeReglement = $facture->reglement;
	$contact = $facture->contact;
	$lines = $facture->_lines;
	
	list($year,$month,$day) = explode("-",substr($facture->date,0,10));
	list($year_e,$month_e,$day_e) = explode("-",$facture->echeance);
	$idfact = $year.$month."-".$facture->facture_id;
	
	$path = PUBLIC_DIR;
	$pdf->setFontHeight(10);
	// Entete 
	// Image
	$pdf->setX(5);$pdf->setY(5);
	if($prefs->entete_logo  == "1"){
	    $pdf->afficheImage($path."/logos/".$_SESSION["user_id"]."/".$prefs->entete,50);    
	}
	if($prefs->entete_text == 1){
	    $pdf->setFontHeight(8);
	    $pdf->afficheText(stripslashes(html_entity_decode(str_replace("<p>","",$prefs->header))),true,3);
	}
	$pdf->setX(120);$pdf->setY(10);
	$pdf->setFontHeight(12);
	$pdf->afficheText("<b>Facture numéro ".$idfact."</b>");
	$pdf->setFontHeight(10);
	$pdf->upY();
	$pdf->afficheText("<b> Le : </b>$day/$month/$year");
	//$pdf->afficheText("<b> Echéance : </b>$day_e/$month_e/$year_e");
	$pdf->afficheText("<b> Type de réglement : </b> ".$typeReglement->libelle);
	if($facture->ref_commande != "") $pdf->afficheText("<b> Référence Commande : </b> ".$facture->ref_commande);
	$pdf->upY(20);
	// Adresse d�stinataire
	if($contact->societe != "") $pdf->afficheText($contact->societe);
	if($contact->nom != "") $pdf->afficheText($contact->prenom." ".$contact->nom);
	$pdf->afficheText($contact->adresse_rue);
	$pdf->afficheText($contact->adresse_cp." ".$contact->adresse_ville);
	// Corps de la page
	// Entete du tableau
	$pdf->setX(5);$pdf->setY(95);
	$pdf->pdf->SetFillColor(230, 230, 230);
	$pdf->pdf->SetDrawColor(100,100, 100);
	$pdf->afficheTh(120,"Désignation");
	$pdf->afficheTh(25,"Quantité");
	$pdf->afficheTh(25,"P.U. HT");
	$pdf->afficheTh(25,"Montant HT");
	$pdf->upY(5);$pdf->setX(5);
	//Corps du tableau
	foreach($lines as $line){
	    $desc = stripslashes(html_entity_decode($line->description));
	    $desc = str_replace("<br />","<br/>",$desc);
	    $desc = str_replace("<br />",'\\n',$desc);
	    $pdf->afficheTd(120,$desc);
	    // Calcule
	    $Y = (strlen($line->description)*1.5);
	    $upY = (ceil(($Y/120))*5);
	    $Y = $pdf->afficheTd(25,$line->qte,"C",1,$upY);
	    $Y = $pdf->afficheTd(25,$line->montant_ht.$euro,"C",1,$upY);
	    $Y = $pdf->afficheTd(25,($line->montant_ht*$line->qte).$euro,"C",1,$upY);
	    $pdf->upY($upY);$pdf->setX(5);
	}
	$pdf->setX(135);$pdf->upY(10);
	$pdf->afficheTh(40,"Total HT ","R");
	$pdf->afficheTd(25,$facture->montant_ht.$euro,"C");
	$pdf->upY(5);$pdf->setX(135);
	if($facture->montant_55 > 0){
	    $pdf->afficheTh(40," Montant TVA 5.5%","R");
	    $pdf->afficheTd(25,$facture->montant_55.$euro,"C");
	    $pdf->upY(5);$pdf->setX(135);
	}
	if($facture->montant_196 > 0){
	    $pdf->afficheTh(40," Montant TVA 19,6%","R");
	    $pdf->afficheTd(25,$facture->montant_196.$euro,"C");
	    $pdf->upY(5);$pdf->setX(135);
	}
	if($facture->montant_remise > 0){
	    $pdf->afficheTh(40," Remise ".$facture->taux_remise."%","R");
	    $pdf->afficheTd(25,$facture->montant_remise.$euro,"C");
	    $pdf->upY(5);$pdf->setX(135);
	}
	if($facture->accompte > 0){
	    $accompteMontant = ceil($facture->montant_ttc*($facture->accompte/100));
	    $pdf->afficheTh(40," Accompte","R");
	    $pdf->afficheTd(25,$accompteMontant.$euro,"C");
	    $pdf->upY(5);$pdf->setX(135);
	}
	$pdf->afficheTh(40,"Total TTC ","R");
	$pdf->afficheTd(25,$facture->montant_ttc.$euro,"C");
	// Infos compl�mentaires
	$pdf->setX(10);$pdf->setY(245);
	$pdf->setWidth(100);$pdf->setFontHeight(9);
	$pdf->afficheText(utf8_encode(stripslashes(html_entity_decode($prefs->infos_compl))),true,4);
	// Pied de page
	$pdf->setX(5);$pdf->setY(280);
	$pdf->setWidth(190);$pdf->setFontHeight(9);
	$pdf->afficheTd(190,"".utf8_encode(stripslashes(html_entity_decode($prefs->footer)))."","C",0);
	
	if($n != count($factures)-1)
		$pdf->sautPage();
}
$pdf->output("Factures.pdf","I");

?>